package com.cn.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.cn.bean.SecondKill;
import com.cn.service.SecondKillService;
import com.google.gson.Gson;

public class SecondKillAction {

	private SecondKill secondKill;
	private SecondKillService secondKillService=null;
	HttpServletResponse response;
	private String parentId;
	private String level;
	ArrayList<Map<String, Object>> list;
	
	public String getParentId() {
		return parentId;
	}
	public void setParentId(String parentId) {
		this.parentId = parentId;
	}
	public String getLevel() {
		return level;
	}
	public void setLevel(String level) {
		this.level = level;
	}
	public ArrayList<Map<String, Object>> getList() {
		return list;
	}
	public void setList(ArrayList<Map<String, Object>> list) {
		this.list = list;
	}
	public SecondKill getSecondKill() {
		return secondKill;
	}
	public void setSecondKill(SecondKill secondKill) {
		this.secondKill = secondKill;
	}
	public SecondKillService getSecondKillService() {
		return secondKillService;
	}
	public void setSecondKillService(SecondKillService secondKillService) {
		this.secondKillService = secondKillService;
	}
	
	/**
	 * 将秒杀数据发送到安卓端
	 */
	public void getSecondKills(){
		try {
		List list = secondKillService.getSecondKills();
		Gson gson = new Gson();
		String json = gson.toJson(list);
		response = ServletActionContext.getResponse();
		response.setContentType("text/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.write(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获取目录和服务商二级联动
	 * @return
	 */
	public String getcatalogProvider(){
		this.setList(secondKillService.getcatalogProvider(parentId, level));
		return "success";
	}
	
	/**
	 * 设置秒杀活动
	 * @return
	 */
	public String setSecondKill(){
		System.out.println("sc_id="+secondKill.getServicecatalog().getId());
		System.out.println("pro_id="+secondKill.getProvider().getId());
		System.out.println("time="+secondKill.getTime());
		if(secondKillService.setSecondKill(secondKill)){
			return "success";
		}
		else{
			return "fail";
		}
		
	}
	
}
